import { isEditableArea, toCells, isSameType } from ".";

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        context.suspendPaint();
        try {
            /**
             * sheetName: sheet.name(),
             * sheet
                        fromSheet: this.fromSheet,
                        fromRanges: [fromRanges],
                        pastedRanges: [pastedRange],
                        isCutting: false,
                        clipboardText: ""
             */
            let { sheet, sheetName, fromSheet: sourceSheet, toRange: cellRange, oldProp, fromRanges: sourceRange, value } = options;
            let {row:trow, col:tcol, colCount:tcolCount, rowCount:trowCount} = cellRange;
            if(trow == -1 || tcol == -1) {
                if(trow == -1) {
                    trow = 0
                }
                if(tcol == -1) {
                    tcol = 0
                }
                cellRange = sheet.getRange(trow, tcol, trowCount, tcolCount);
            }
            if (isUndo) {


                const {row:rangerow, col:rangecol, colCount:rangecolCount, rowCount:rangerowCount} = cellRange;
                for(let r = rangerow ; r < rangerow + rangerowCount; r++) {
                    for(let c = rangecol; c< rangecol + rangecolCount; c++) {
                        const span = sheet.getSpan(r,c);
                            if(span){
                                sheet.removeSpan(r,c);
                            }
                    }
                }

                options._oldSpans.forEach(range => {
                    sheet.addSpan(range.row, range.col, range.rowCount, range.colCount);
                });
                const targetCells = toCells({ ...cellRange, sheet });
                targetCells.forEach((cell, index) => {
                    cell.backColor((oldProp[index]).backColor);
                    //cell.backgroundImageLayout((oldProp[index]).backgroundImageLayout);
                    cell.borderBottom((oldProp[index]).borderBottom);
                    cell.borderLeft((oldProp[index]).borderLeft);
                    cell.borderRight((oldProp[index]).borderRight);
                    cell.borderTop((oldProp[index]).borderTop);
                    cell.font((oldProp[index].font));
                    cell.foreColor((oldProp[index]).foreColor);
                    cell.formatter((oldProp[index]).formatter);
                    cell.hAlign((oldProp[index]).hAlign);
                    cell.vAlign((oldProp[index]).vAlign);
                    cell.themeFont((oldProp[index]).themeFont);
                    cell.wordWrap((oldProp[index]).wordWrap);
                    cell.textDecoration((oldProp[index]).textDecoration);
                });
            } else {
                    const cells = toCells({ ...cellRange, sheet });
                    options.oldProp = cells.map(cell => {
                    return(
                        {
                            backColor: cell.backColor(),
                            //backgroundImageLayout: cell.backgroundImageLayout(),
                            borderBottom: cell.borderBottom(),
                            borderLeft: cell.borderLeft(),
                            borderRight: cell.borderRight(),
                            borderTop: cell.borderTop(),
                            font: cell.font(),
                            foreColor: cell.foreColor(),
                            formatter: cell.formatter(),
                            hAlign: cell.hAlign(),
                            vAlign: cell.vAlign(),
                            themeFont: cell.themeFont(),
                            wordWrap: cell.wordWrap(),
                            textDecoration:cell.textDecoration(),
                        }
                    )

                });
                
                options._oldSpans = [];
                const {row:rangerow, col:rangecol, colCount:rangecolCount, rowCount:rangerowCount} = cellRange;
                const existsSpan = []
                for(let r = rangerow ; r < rangerow + rangerowCount; r++) {
                    for(let c = rangecol; c< rangecol + rangecolCount; c++) {
                        const span = sheet.getSpan(r,c);
                            if(span && existsSpan.indexOf(span.row + "@" + span.col) == -1){
                                options._oldSpans.push(span);
                                existsSpan.push(span.row + "@" + span.col);
                            }
                    }
                }

                let rowStep = sourceRange.rowCount, colStep = sourceRange.colCount;
                let endRow = cellRange.row + cellRange.rowCount - 1, endCol = cellRange.col + cellRange.colCount - 1;

            // if toRange bigger than fromRange, repeat paint
            for(let startRow = cellRange.row; startRow <= endRow; startRow = startRow + rowStep){
                for(let startCol = cellRange.col; startCol <= endCol; startCol = startCol + colStep){

                    let rowCount = startRow + rowStep > endRow + 1 ? endRow - startRow + 1 : rowStep;
                    let colCount = startCol + colStep > endCol + 1 ? endCol - startCol + 1 : colStep;
                    // sheet.copyTo(fromRange.row,fromRange.col, startRow, startCol, rowCount, colCount,GC.Spread.Sheets.CopyToOptions.style | GC.Spread.Sheets.CopyToOptions.span);
                    let fromRanges = new window.GC.Spread.Sheets.Range(sourceRange.row, sourceRange.col, rowCount, colCount);
                    let pastedRange = new window.GC.Spread.Sheets.Range(startRow, startCol, rowCount, colCount);

                    for(let r = pastedRange.row ; r < pastedRange.row + pastedRange.rowCount ; r++) {
                        for(let c = pastedRange.col; c < pastedRange.col + pastedRange.colCount ; c++) {

                            const span = sheet.getSpan(r,c);
                            if(span){
                                sheet.removeSpan(r,c);
                                console.log("删除合并")
                            }
                        }
                    }
                    const diffRow = fromRanges.row - pastedRange.row;
                    const diffCol = fromRanges.col - pastedRange.col; 
                    for(let r = fromRanges.row ; r < fromRanges.row + rowCount ; r++) {
                        for(let c = fromRanges.col; c < fromRanges.col + colCount ; c++) {

                            const span = sheet.getSpan(r,c);
                            if(span && span.col == c && span.row == r){
                                sheet.addSpan(span.row - diffRow, span.col - diffCol, 
                                    span.rowCount <= rowCount ? span.rowCount : rowCount, 
                                    span.colCount <= colCount ? span.colCount : colCount);
                            }
                        }
                    }

                    const tmptargetCells = toCells({ ...pastedRange, sheet });
                    if (sourceSheet && pastedRange) {
                        const sourceCells = toCells({ ...fromRanges, sheet: sourceSheet });
                        let getSourceCell;
                        if (sourceCells.length === 1) {
                            getSourceCell = () => {
                                return sourceCells[0]
                            }
                        } else {
                            getSourceCell = (index) => {
                                return sourceCells[index];
                            }
                        }

                        
                        tmptargetCells.forEach((cell, index) => {
                            const sourceCell = getSourceCell(index);
                            if (sourceCell) {
                                cell.backColor(sourceCell.backColor());
                                //cell.backgroundImageLayout(sourceCell.backgroundImageLayout());
                                cell.borderBottom(sourceCell.borderBottom());
                                cell.borderLeft(sourceCell.borderLeft());
                                cell.borderRight(sourceCell.borderRight());
                                cell.borderTop(sourceCell.borderTop());
                                cell.font(sourceCell.font());
                                cell.foreColor(sourceCell.foreColor());
                                cell.formatter(sourceCell.formatter());
                                cell.hAlign(sourceCell.hAlign());
                                cell.vAlign(sourceCell.vAlign());
                                cell.themeFont(sourceCell.themeFont());
                                cell.wordWrap(sourceCell.wordWrap());
                                cell.textDecoration(sourceCell.textDecoration());
    
                            }
                        });
                    }
                }
            }
                
                
            }
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
        context.resumePaint();
    }
};